using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace SuDokuSolution
{
    /// <summary>
    /// The graphicsl cell to be drawn on the screen, containing cell
    /// data and mouse events
    /// </summary>
    public class Cell_9_by_9 : GraphicalCell
    {



        /// <summary>
        /// Reference to the parent grid of the  cell
        /// </summary>
        private GridControl_9_by_9 parentGrid;
        Constants one1 = new Constants1();


        public Cell_9_by_9()
        {

        }
        public Cell_9_by_9(GridControl_9_by_9 gc, Brush b)
        {
            one = new Constants1();
            this.parentGrid = gc;
            this.backGroundBrush = b;
            this.CheckedNbs = new ArrayList(one1.nbOfCells);
            this.cellData = new CellData();
        }



     
        /// Method to paint a cell
        

        public virtual void Paint(Graphics graphics)
        {
            //fill background
            graphics.FillRectangle(backGroundBrush, cellRect);

            if (!redraw)
                return;

            Brush selBrush;
            if (cellData.problemNb == one1.NoNumber)
            {
                //draw vertical lines
                for (int i = 1; i < one1.nbOfLines; i++)
                {
                    graphics.DrawLine(DrawingTools.LightPen,
                        cellLocation.X + subCellSize.Width * i, cellLocation.Y + 1,
                        cellLocation.X + subCellSize.Width * i, cellLocation.Y + cellSize.Height - 1);
                }

                //draw horizontal lines
                for (int i = 1; i < one1.nbOfLines; i++)
                {
                    graphics.DrawLine(DrawingTools.LightPen,
                        cellLocation.X + 1, cellLocation.Y + subCellSize.Height * i,
                        cellLocation.X + cellSize.Width - 1, cellLocation.Y + subCellSize.Height * i);
                }

                //draw marked nbs
                foreach (Object ob in this.CheckedNbs)
                {
                    int nb = (int)ob;
                    if (this.cellData.candidateNb.Contains(nb) || !parentGrid.simultaneousCheck)            //if valid nb
                        selBrush = DrawingTools.AnswerBrush;
                    else
                        selBrush = DrawingTools.ErrorBrush;

                    SizeF fontSize = graphics.MeasureString(nb.ToString(), DrawingTools.miniSelectedFont);
                    graphics.DrawString(nb--.ToString(), DrawingTools.miniSelectedFont,
                        selBrush, cellLocation.X + subCellSize.Width * (nb % one1.nbOfLines) + (subCellSize.Width - fontSize.Width) / 2,
                        cellLocation.Y + subCellSize.Height * (nb / one1.nbOfLines) + (subCellSize.Height - fontSize.Height) / 2);
                }
            }

            else
            {

                if (cellData.readOnly)
                    selBrush = DrawingTools.ProblemBrush;
                else if (!MarkError || !parentGrid.simultaneousCheck)
                    selBrush = DrawingTools.AnswerBrush;
                else
                    selBrush = DrawingTools.ErrorBrush;

                SizeF fontSize = graphics.MeasureString(cellData.problemNb.ToString(), DrawingTools.SelectedFont);

                graphics.DrawString(cellData.problemNb.ToString(), DrawingTools.SelectedFont,
                    selBrush, cellLocation.X + (cellSize.Width - fontSize.Width) / 2,
                    cellLocation.Y + (cellSize.Height - fontSize.Height) / 2);
            }
        }
 	
       

     
        /// Processes Mouse double click event 
               
        public virtual void MouseDown(MouseEventArgs e)
        {
            if (cellData.readOnly)
                return;

            int clickedNb = getClickedNb(e.X - cellLocation.X, e.Y - cellLocation.Y);
            if (cellData.problemNb == one1.NoNumber)
            {
                if (CheckedNbs.Contains(clickedNb))
                    CheckedNbs.Remove(clickedNb);
                else
                    CheckedNbs.Add(clickedNb);
            }
            else { } //do nothing
            parentGrid.RedrawCell(this);
        }

    }
}
